package thread;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


class MyThreadPool{
    private BlockingQueue<Runnable>queue=new LinkedBlockingQueue<>();

    //通过这个方法，来把任务添加到线程池中
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    //n 表示线程池里有几个线程
    //创建了一个固定数量的线程池
    public MyThreadPool(int n){
        for (int i = 0; i <n ; i++) {
            Thread t =new Thread(()->{
               while (true){
                   try {
                       Runnable runnable = queue.take();
                       runnable.run();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }

               }
            });
            t.start();
        }
    }
}
public class demo6 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool = new MyThreadPool(4);
        for (int i = 0; i <1000 ; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+" hello");
                }
            });
        }
    }
}
